package cn.maxwell.pet.controller;

import cn.maxwell.basic.util.JsonResult;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.pet.domain.Pet;
import cn.maxwell.pet.domain.PetDetail;
import cn.maxwell.pet.query.PetDetailQuery;
import cn.maxwell.pet.query.PetQuery;
import cn.maxwell.pet.service.IPetDetailService;
import cn.maxwell.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petDetail")
@Api(value = "宠物明细的API",description="宠物明细相关的CRUD功能")
public class PetDetailController {

    @Autowired
    private IPetDetailService petDetailService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping //get请求
    @ApiOperation(value = "查询所有" )
    public List<PetDetail> loadAll(){
        return petDetailService.loadAll();
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}") //get请求，携带id参数
    @ApiOperation(value = "通过ID查询" )
    //swagger注解：paramType="path"：解析path路径携带的参数；dataType="Long"：指定参数类型
    @ApiImplicitParam(name="id",value="编号",required = true,paramType="path",dataType="Long")
    public PetDetail loadById(@PathVariable("id") Long id){//@PathVariable：获取path路径携带的参数
        return petDetailService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除操作" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            petDetailService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     * @param petDetail
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "添加或修改操作" )
    public JsonResult save(@RequestBody PetDetail petDetail){//@RequestBody:在请求体中获取参数
        try {
            if(petDetail.getId()==null){
                petDetailService.add(petDetail);
            }else{
                petDetailService.update(petDetail);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param petDetailQuery
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "分页查询" )
    public PageList<PetDetail> query(@RequestBody PetDetailQuery petDetailQuery){
        return petDetailService.queryPage(petDetailQuery);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){ //List<Long>:要删除的id的集合
        try {
            petDetailService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
}
